﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonFunc;

public partial class Manageweb_Manage_WebInfo : System.Web.UI.Page
{
    MODEL.WebInfo MWI = new MODEL.WebInfo();
    MODEL.WebInfo _MWI = new MODEL.WebInfo();

    BLL.WebInfo BWI = new BLL.WebInfo();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        ClickInit();
    }


    #region 按钮事件初始化
    protected void ClickInit()
    {
        LB_WebInfo.Text = "";

        TABLE_Add_Mod.Visible = false;

        try
        {
            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);

                LB_TableTitle.Text = "添加信息";
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);

                LB_TableTitle.Text = "修改信息";
            }
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion

    
    #region 页面数据初始化
    protected void TableDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";

                TB_Content.Text = "";
            }
            else
            {
                MWI.WI_ID = Convert.ToInt32(LB_ID.Text);

                _MWI = BWI.GetDataByID(MWI);

                TB_Title.Text = _MWI.WI_Title;

                TB_Content.Text = CommonFunction.SubStr(
                    CommonFunction.RegexReplace(_MWI.WI_Content),
                    10,
                    "……");
            }

            
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion


    #region 内容字符串 [格式化]
    protected string ContentSubStr(object _Content)
    {
        return CommonFunction.SubStr(
            CommonFunction.RegexReplace(
            _Content.ToString()),
            10,
            "…");
    }
    #endregion


    #region 添加信息 [GridView] [Commmand]
    protected void Btn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            TableDataInit();
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion


    #region 修改信息 [GridView] [Commmand]
    protected void Btn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            TableDataInit();
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion


    #region 删除信息 [GridView] [Commmand]
    protected void Btn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MWI.WI_ID = Convert.ToInt32(e.CommandArgument);

            BWI.Del(MWI);

            LB_WebInfo.Text = "删除成功";

            GV_WebInfoList.DataBind();
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion


    #region 添加信息 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MWI.WI_Title = TB_Title.Text;
            MWI.WI_Content = TB_Content.Text;
            MWI.WI_DateTime = DateTime.Now;
            MWI.WI_State = 0;
            MWI.WI_Hit = 0;

            BWI.Add(MWI);

            LB_WebInfo.Text = "添加成功";

            GV_WebInfoList.DataBind();
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion


    #region 修改信息 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MWI.WI_Title = TB_Title.Text; 
            MWI.WI_Content = TB_Content.Text;
            MWI.WI_ID = Convert.ToInt32(LB_ID.Text);

            BWI.Mod(MWI);

            LB_WebInfo.Text = "修改成功";

            GV_WebInfoList.DataBind();
        }
        catch (Exception ex)
        {
            LB_WebInfo.Text = ex.Message;
        }
    }
    #endregion
}

